package com.zhibang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhibang.entity.Account;
import com.zhibang.entity.Menu;
import com.zhibang.service.MenuService;
import com.zhibang.util.Result;
import com.zhibang.vo.IndexMenuVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @autor 流萤微微
 * @date 2025年03月06日 9:48
 */
@RestController
@RequestMapping("menu")
//@RequiresUser
public class MenuController {
    @Resource
    MenuService ms;

    @RequestMapping("initMenu")
    public Result init(){
//        拿到登录用户对象
        Subject subject = SecurityUtils.getSubject();
        Account account=(Account) subject.getPrincipal();
        List<IndexMenuVo> vos = ms.initMenu(account.getId());
        return Result.data(vos);
    }

    @RequestMapping("typeCount")
    @RequiresGuest//游客访问
    public Result typeCount(){
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.select("type name,count(*) value");
        qw.groupBy("type");
        List<Map<String, Object>> maps = ms.listMaps(qw);
        return Result.data(maps);
    }
}
